﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Windows.Input;
using System.Linq;
using System.Text;
using MachineQA.DataAccess;
using System.Data.Objects.DataClasses;
using MachineQA.Properties;
using MachineQA.Model;
using System.Collections.ObjectModel;

namespace MachineQA.ViewModel
{
    /// <summary>
    /// UI-friendly wrapper around a Energy object
    /// </summary>
    class EnergyViewModel : WorkspaceViewModel, IDataErrorInfo
    {
        readonly Energy _energy;
        readonly EnergyRepository _energyRepository;
        bool _isSelected = false;
        RelayCommand _saveCommand;

        public EnergyViewModel(Energy energy, EnergyRepository energyRepository)
        {
            _energy = energy;
            _energyRepository = energyRepository;
        }

        #region Presentation Properties

        public override string DisplayName
        {
            get
            {
                return Resources.EnergyViewModel_DisplayName;
            }
        }

        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                if (value == _isSelected)
                    return;

                _isSelected = value;

                base.OnPropertyChanged(Resources.IsSelectedPropertyName);
            }
        }

        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new RelayCommand(
                        param => this.Save(),
                        param => this.CanSave
                            );
                }
                return _saveCommand;
            }
        }

        #endregion // Presentation Properties

        public Modality Modality
        {
            get { return _energy.Modality; }
            set
            {
                if (value == _energy.Modality)
                    return;

                _energy.Modality = value;
                base.OnPropertyChanged("Modality");
            }
        }

        public double Value
        {
            get { return _energy.value; }
            set
            {
                if (value == _energy.value)
                    return;

                _energy.value = value;
                base.OnPropertyChanged("Value");
            }
        }

        #region IDataErrorInfo

        public string Error
        {
            get { return (_energy as IDataErrorInfo).Error; }
        }

        public string this[string propertyName]
        {
            get 
            {
                return (_energy as IDataErrorInfo).Error;
            }
        }

        #endregion

        #region Public Methods

        public void Save()
        {
            if (!this.CanSave)
                throw new InvalidOperationException();

            if (this.IsNewEnergy)
                _energyRepository.AddEnergy(_energy);

            base.OnPropertyChanged("DisplayName");
        }

        #endregion // Public Methods

#region Helper Functions

        bool IsNewEnergy
        {
            get { return !_energyRepository.ContainsEnergy(_energy); }
        }

        bool CanSave
        {
            get { return _energy.IsValid; }
        }
#endregion // Helper Functions
    }
}
